package com.xidian.zhiancloud.service.exam.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.exam.TExamPaperMapper;
import com.xidian.zhiancloud.model.exam.ExamQuestionAnswer;
import com.xidian.zhiancloud.model.exam.ExamTypeCount;
import com.xidian.zhiancloud.model.exam.TExamConfig;
import com.xidian.zhiancloud.model.exam.TExamPaper;
import com.xidian.zhiancloud.model.exam.TExamQuestion;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.exam.ITExamPaperService;
import com.xidian.zhiancloud.util.api.DateUtils;

/**
 * 试卷Service业务层处理
 * 
 * @author zhouweitao
 * @date 2021-03-05
 */
@Service
public class TExamPaperServiceImpl implements ITExamPaperService 
{
    @Autowired
    private TExamPaperMapper tExamPaperMapper;

    /**
     * 查询试卷
     * 
     * @param id 试卷ID
     * @return 试卷
     */
    @Override
    public TExamPaper selectTExamPaperById(Long id)
    {
        return tExamPaperMapper.selectTExamPaperById(id);
    }

    /**
     * 查询试卷列表
     * 
     * @param tExamPaper 试卷
     * @return 试卷
     */
    @Override
    public Page<TExamPaper> selectTExamPaperList(TExamPaper tExamPaper)
    {
    	PageHelper.startPage(tExamPaper.getPageNum()-1, tExamPaper.getPageSize());
    	return tExamPaperMapper.selectTExamPaperList(tExamPaper);
    }

    /**
     * 新增试卷
     * 
     * @param tExamPaper 试卷
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTExamPaper(TExamPaper tExamPaper)
    {
    	
        tExamPaper.setCreateTime(DateUtils.getNowDate());
        tExamPaper.setUpdateTime(DateUtils.getNowDate());
        tExamPaperMapper.insertTExamPaper(tExamPaper);
        if(tExamPaper.getType().equals("1")) {
        	if(tExamPaper.getConfig()!=null) {
        		tExamPaper.getConfig().setPaperId(tExamPaper.getId());
            	tExamPaperMapper.insertPaperConfig(tExamPaper.getConfig());
        	}
        	
        }else {
        	if(tExamPaper.getQuestions()!=null && tExamPaper.getQuestions().size()>0) {
        		for (int i = 0; i < tExamPaper.getQuestions().size(); i++) {
        			tExamPaper.getQuestions().get(i).setPeperId(tExamPaper.getId());
				}
        		tExamPaperMapper.insertPaperQuestions(tExamPaper.getQuestions());
        	}
        }
        return 1;
    }

    /**
     * 修改试卷
     * 
     * @param tExamPaper 试卷
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTExamPaper(TExamPaper tExamPaper)
    {
        tExamPaper.setUpdateTime(DateUtils.getNowDate());
        if(tExamPaper.getType().equals("1") && tExamPaper.getConfig() != null) {
        	
        	tExamPaperMapper.updateConfig(tExamPaper.getConfig());
        }else {
        	if(tExamPaper.getQuestions()!=null && tExamPaper.getQuestions().size()>0) {
        		for (int i = 0; i < tExamPaper.getQuestions().size(); i++) {
        			tExamPaper.getQuestions().get(i).setPeperId(tExamPaper.getId());
				}
        		Long[] abc = {tExamPaper.getId()};
        		tExamPaperMapper.deletePaperQuestionByIds(abc);
        		tExamPaperMapper.insertPaperQuestions(tExamPaper.getQuestions());
        	}
        }
        return tExamPaperMapper.updateTExamPaper(tExamPaper);
    }

    /**
     * 批量删除试卷
     * 
     * @param ids 需要删除的试卷ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTExamPaperByIds(Long[] ids)
    {
    	tExamPaperMapper.deletePaperQuestionByIds(ids);
        return tExamPaperMapper.deleteTExamPaperByIds(ids);
    }

    /**
     * 删除试卷信息
     * 
     * @param id 试卷ID
     * @return 结果
     */
    @Override
    public int deleteTExamPaperById(Long id)
    {
        return tExamPaperMapper.deleteTExamPaperById(id);
    }

	@Override
	public Page<ExamTypeCount> getTypeCount(BaseReqQuery query) {
		// TODO Auto-generated method stub
		PageHelper.startPage(query.getPageNum()-1, query.getPageSize());
		return tExamPaperMapper.getTypeCount( query);
	}

	@Override
	public List<TExamQuestion> getPaper(Long paperId, String type) {
		// TODO Auto-generated method stub
		List<TExamQuestion> questions = new ArrayList<>();
		if(type.equals("1")) {
			TExamConfig config = tExamPaperMapper.getPaperConfig(paperId);
			questions = tExamPaperMapper.getPaperByConfig(config);
		}else {
			questions = tExamPaperMapper.getPaper(paperId);
		}
		return questions;
	}

	@Override
	public int insertTExamPaperAnswers(List<ExamQuestionAnswer> answers) {
		// TODO Auto-generated method stub
		return tExamPaperMapper.insertTExamPaperAnswers(answers);
	}

	@Override
	public List<TExamQuestion> getPaperAnswers(Long paperId,String type, Long userId) {
		// TODO Auto-generated method stub
		List<TExamQuestion> questions = new ArrayList<>();
		if(type.equals("1")) {
			questions = tExamPaperMapper.getPaperAnswers2(paperId, userId);
		}else {
			questions = tExamPaperMapper.getPaperAnswers(paperId,userId);
		}
		return questions;
	}
	
	
}
